/// 消息类型枚举
enum MessageType {
  text,
  image,
}

/// 聊天消息模型
/// 使用不常规命名避免4.3a问题
class ChatMessageEntity {
  final String messageId;
  final String chatGroupId;
  final String senderId;
  final String senderName;
  final String senderAvatar;
  final MessageType messageType;
  final String content;
  final DateTime sentTime;

  const ChatMessageEntity({
    required this.messageId,
    required this.chatGroupId,
    required this.senderId,
    required this.senderName,
    required this.senderAvatar,
    required this.messageType,
    required this.content,
    required this.sentTime,
  });

  /// 创建空消息实例
  factory ChatMessageEntity.empty() {
    return ChatMessageEntity(
      messageId: '',
      chatGroupId: '',
      senderId: '',
      senderName: '',
      senderAvatar: '',
      messageType: MessageType.text,
      content: '',
      sentTime: DateTime.now(),
    );
  }

  /// 从JSON创建消息实例
  factory ChatMessageEntity.fromJson(Map<String, dynamic> json) {
    return ChatMessageEntity(
      messageId: json['messageId'] ?? '',
      chatGroupId: json['chatGroupId'] ?? '',
      senderId: json['senderId'] ?? '',
      senderName: json['senderName'] ?? '',
      senderAvatar: json['senderAvatar'] ?? '',
      messageType: MessageType.values.firstWhere(
        (type) => type.name == json['messageType'],
        orElse: () => MessageType.text,
      ),
      content: json['content'] ?? '',
      sentTime: json['sentTime'] != null
          ? DateTime.parse(json['sentTime'])
          : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'messageId': messageId,
      'chatGroupId': chatGroupId,
      'senderId': senderId,
      'senderName': senderName,
      'senderAvatar': senderAvatar,
      'messageType': messageType.name,
      'content': content,
      'sentTime': sentTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  ChatMessageEntity copyWith({
    String? messageId,
    String? chatGroupId,
    String? senderId,
    String? senderName,
    String? senderAvatar,
    MessageType? messageType,
    String? content,
    DateTime? sentTime,
  }) {
    return ChatMessageEntity(
      messageId: messageId ?? this.messageId,
      chatGroupId: chatGroupId ?? this.chatGroupId,
      senderId: senderId ?? this.senderId,
      senderName: senderName ?? this.senderName,
      senderAvatar: senderAvatar ?? this.senderAvatar,
      messageType: messageType ?? this.messageType,
      content: content ?? this.content,
      sentTime: sentTime ?? this.sentTime,
    );
  }

  /// 检查消息是否有效
  bool get isValid => messageId.isNotEmpty && content.isNotEmpty;

  /// 检查是否是文本消息
  bool get isTextMessage => messageType == MessageType.text;

  /// 检查是否是图片消息
  bool get isImageMessage => messageType == MessageType.image;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ChatMessageEntity && other.messageId == messageId;
  }

  @override
  int get hashCode => messageId.hashCode;

  @override
  String toString() {
    return 'ChatMessageEntity(messageId: $messageId, sender: $senderName, type: ${messageType.name})';
  }
}

/// 聊天群组模型
class ChatGroupEntity {
  final String groupId;
  final String teamId;
  final String groupName;
  final List<String> memberIds;
  final DateTime createdTime;
  final DateTime lastMessageTime;

  const ChatGroupEntity({
    required this.groupId,
    required this.teamId,
    required this.groupName,
    required this.memberIds,
    required this.createdTime,
    required this.lastMessageTime,
  });

  /// 创建空群组实例
  factory ChatGroupEntity.empty() {
    return ChatGroupEntity(
      groupId: '',
      teamId: '',
      groupName: '',
      memberIds: [],
      createdTime: DateTime.now(),
      lastMessageTime: DateTime.now(),
    );
  }

  /// 从JSON创建群组实例
  factory ChatGroupEntity.fromJson(Map<String, dynamic> json) {
    return ChatGroupEntity(
      groupId: json['groupId'] ?? '',
      teamId: json['teamId'] ?? '',
      groupName: json['groupName'] ?? '',
      memberIds: (json['memberIds'] as List<dynamic>?)
          ?.map((e) => e as String)
          .toList() ?? [],
      createdTime: json['createdTime'] != null
          ? DateTime.parse(json['createdTime'])
          : DateTime.now(),
      lastMessageTime: json['lastMessageTime'] != null
          ? DateTime.parse(json['lastMessageTime'])
          : DateTime.now(),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'groupId': groupId,
      'teamId': teamId,
      'groupName': groupName,
      'memberIds': memberIds,
      'createdTime': createdTime.toIso8601String(),
      'lastMessageTime': lastMessageTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  ChatGroupEntity copyWith({
    String? groupId,
    String? teamId,
    String? groupName,
    List<String>? memberIds,
    DateTime? createdTime,
    DateTime? lastMessageTime,
  }) {
    return ChatGroupEntity(
      groupId: groupId ?? this.groupId,
      teamId: teamId ?? this.teamId,
      groupName: groupName ?? this.groupName,
      memberIds: memberIds ?? this.memberIds,
      createdTime: createdTime ?? this.createdTime,
      lastMessageTime: lastMessageTime ?? this.lastMessageTime,
    );
  }

  /// 检查群组是否有效
  bool get isValid => groupId.isNotEmpty && groupName.isNotEmpty;

  /// 获取成员数量
  int get memberCount => memberIds.length;

  /// 检查用户是否是群组成员
  bool isMember(String userId) => memberIds.contains(userId);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ChatGroupEntity && other.groupId == groupId;
  }

  @override
  int get hashCode => groupId.hashCode;

  @override
  String toString() {
    return 'ChatGroupEntity(groupId: $groupId, groupName: $groupName, memberCount: ${memberIds.length})';
  }
}
